package org.platform.lwc.netty.controller;

import cn.hutool.json.JSONUtil;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.platform.lwc.netty.constant.NettyConstant;
import org.platform.lwc.netty.controller.request.PushMsgReq;
import org.platform.lwc.netty.entity.PushMsgEntity;
import org.platform.lwc.tool.api.R;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping("/push")
@AllArgsConstructor
@Slf4j
public class PushController {

    private RedisTemplate redisTemplate;

    /**
     * @Description: 推送消息，指定用户id
     * @Author: lwc
     * @Date: 2022-04-22 11:01
     */
    @PostMapping("/user")
    public R pushUser(@RequestBody PushMsgReq pushMsgReq) {
        // 发布到redis中
        PushMsgEntity pushMsgEntity = PushMsgEntity.builder()
                .userId(pushMsgReq.getUserId()).message(pushMsgReq.getMessage()).build();
        redisTemplate.convertAndSend(NettyConstant.REDIS_CHANNEL_INDEX, JSONUtil.toJsonStr(pushMsgEntity));
        return R.success("ok");
    }
}
